package com.example.maptext;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FileStoreTool {
    private static final String TAG = "FileStoreTool";
    private static ArrayList<String> newList;
    private Context mContext;

    public FileStoreTool(Context context){
        this.mContext=context;
    }

    /**
     *
     * 保存文件的位置
     *
     */
    public String getSDPath() {
        boolean hasSDCard = Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
        if (hasSDCard) {
            return Environment.getExternalStorageDirectory().toString();
        } else {
            return Environment.getDownloadCacheDirectory().toString();
        }
    }

    /**
     *
     * 保存内容到文件
     *
     */
    public boolean saveFile(String str, String filePath) {
        boolean isSaved=false;
        FileOutputStream fos = null;
        try {
            File file = new File(filePath);
            if (!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file,true);
            str+="\r\n"; //换行符
            fos.write(str.getBytes());
            fos.flush();
            isSaved=true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fos)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return isSaved;
    }

    /**
     * 读文件里的内容
     * @param filePath
     * @return
     */
    public String readSDFile(String filePath) {

        StringBuffer sb = new StringBuffer();

        File file = new File(filePath);

        try {

            FileInputStream fis = new FileInputStream(file);

            int c;

            while ((c = fis.read()) != -1) {
                Log.d(TAG,"c  ==  "+c);
                sb.append((char) c);

            }

            fis.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        return sb.toString();

    }

    /**
     * 按行读取内容
     * @param strFilePath
     * @return
     */
    public static ArrayList<String> ReadTxtFile(String strFilePath)
    {
        String path = strFilePath;
        newList=new ArrayList<String>();
        //打开文件
        File file = new File(path);
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory())
        {
            Log.d("TestFile", "The File doesn't not exist.");
        }
        else
        {
            try {
                InputStream instream = new FileInputStream(file);
                if (instream != null)
                {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while (( line = buffreader.readLine()) != null) {
                        Log.d(TAG,"line  ==  "+line);
                        newList.add(line+"\n");
                    }
                    instream.close();
                }
            }
            catch (java.io.FileNotFoundException e)
            {
                Log.d("TestFile", "The File doesn't not exist.");
            }
            catch (IOException e)
            {
                Log.d("TestFile", e.getMessage());
            }
        }
        return newList;
    }
}
